package org.smoozik.services.sequence;

import java.util.ArrayList;

import org.smoozik.services.musictheory.Token;

public class Song {
	private ArrayList<Sequence> sequences;
	private int timeWeight;
	private int nbTimesByMeasure;

	public Song(int timeWeight) {
		sequences = new ArrayList<>();
		this.timeWeight = timeWeight;
	}

	public Song() {
		sequences = new ArrayList<>();
	}

	public ArrayList<Sequence> getSequences() {
		return sequences;
	}

	public ArrayList<Token> getTokens() {
		ArrayList<Token> tokens = new ArrayList<>();

		for (Sequence sequence : sequences) {
			tokens.addAll(sequence.getTokens());
		}

		return tokens;
	}

	public double getLastPlaceFromTokenList() {
		double res = 0;
		double tmp = 0;

		for (Token t : getTokens()) {
			tmp = t.getPlace() + t.getDuration();
			if (tmp > res) {
				res = tmp;
			}
		}

		return res;
	}

	public double getDuration() {
		double last = getLastPlaceFromTokenList();
		return last * timeWeight;
		
	}


	public int getTimeWeight() {
		return timeWeight;
	}

	public void setTimeWeight(int timeWeight) {
		this.timeWeight = timeWeight;
	}

	public int getNbTimesByMeasure() {
		return nbTimesByMeasure;
	}

	public void setNbTimesByMeasure(int nbTimesByMeasure) {
		this.nbTimesByMeasure = nbTimesByMeasure;
	}

	public void setSequences(ArrayList<Sequence> sequences) {
		this.sequences = sequences;
	}

	public String serialize() {
		String str = "";
		str += "<song speed=\"" + timeWeight + "\" nbTimes=\""
				+ nbTimesByMeasure + "\">\n";

		for (Sequence s : this.sequences) {
			str += s.serialize();
		}

		str += "</song>\n";

		return str;
	}

	public String toString() {
		String str = "";

		for (Sequence s : this.sequences) {
			str += s.toString();
		}

		return str;
	}

}
